package summary;

import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/7/7 10:02
 * https://leetcode.cn/problems/largest-rectangle-in-histogram/
 */
public class Title84 {

    public static int largestRectangleArea(int[] heights) {
        int n = heights.length;
        int[] arr = new int[n + 2];
        for (int i = 0; i < n; i++) {
            arr[i + 1] = heights[i];
        }
        heights = arr;
        Stack<Integer> stack = new Stack<>();
        int max = 0;
        for (int i = 0; i < heights.length; i++) {
            //计算连续的面积
            while (!stack.isEmpty() && heights[i] < heights[stack.peek()]) {
                Integer mid = stack.pop();
                int left = stack.peek();
                int h = heights[mid];
                int w = i - left - 1;
                max = Math.max(max, h * w);
            }
            stack.push(i);
        }
        return max;
    }

    public static void main(String[] args) {
        int[] arr = {2, 1, 5, 6, 2, 3};
        largestRectangleArea(arr);
    }
}
